<?php
class ControllerModuleElineaBestSell extends Controller {
	protected function index($setting) {
		$this->load->model('catalog/product');
		$this->load->model('catalog/category');
		$this->load->model('tool/image');
		$this->data['products'] = array();

		
		
		// Best sellers    99
		$filter_category_id = 99;
		$page  = 1;
		$limit = 3;
		$data = array(
			'filter_category_id' => $filter_category_id, 
			'filter_sub_category' => false,
			'sort'               => 'ASC',
			'order'              => 'p.sort_order',
			'start'              => ($page - 1) * $limit,
			'limit'              => $limit
		);
		$category_arr = $this->model_catalog_category->getCategory($filter_category_id); 
		$this->data['category_title'] = $category_arr['name']; 
		$product_total = $this->model_catalog_product->getTotalProducts($data); 
			
		$results = $this->model_catalog_product->getProducts($data);
		
		foreach ($results as $result) {
			
			if ($result['image']) {
				//$image = $this->model_tool_image->resize($result['image'],165, 165);
				$image = HTTP_IMAGE.$result['image'];
			} else {
				$image = false;
			}
			$price_percent = 0;
			if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
				$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$price = false;
			}
			
			if ((float)$result['special']) {
				$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
				$price_1 =$this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'));
				$price_2 =$this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')); 
				$price_percent = ( ($price_2 - $price_1) / $price_2  ) * 100 ;
				$price_percent = round($price_percent);
			} else {
				$special = false;
			}	
			
			if ($this->config->get('config_tax')) {
				$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
			} else {
				$tax = false;
			}				
			
			if ($this->config->get('config_review_status')) {
				$rating = (int)$result['rating'];
			} else {
				$rating = false;
			}
							
			$this->data['products'][] = array(
				'product_id'  => $result['product_id'],
				'image'       => $image,
				'name'        => $this->commonfunc->trunchtml($result['name'],25,'...'),
				'is_new'        => $result['is_new'],
				'description' => mb_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
				'price'       => $price,
				'special'     => $special,
				'price_percent'     => $price_percent,
				'tax'         => $tax,
				'rating'      => $result['rating'],
				'ratingproduct'      => $result['ratingproduct'],
				'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
				'href'        => $this->url->link('product/product', 'path='.$filter_category_id.'&product_id=' . $result['product_id'])
			);
			
		}
		//print_r($this->data['products']);
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/elinea_best_sell.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/elinea_best_sell.tpl';
		} else {
			$this->template = 'default/template/module/elinea_best_sell.tpl';
		}

		$this->render();
	}
}
?>